/**
  ******************************************************************************
  * @file    drv_modbus.h
  * @author  Forester
  * @brief   
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */
	
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BMS_HAL_COMM_H
#define __BMS_HAL_COMM_H

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>

/* Config Defines ------------------------------------------------------------*/
#define SYSTEM_SUPPORT 1

/* Defines -------------------------------------------------------------------*/
#define MODBUSRTU_DATA_MINIUM_LENGTH 6

/* Exported types ------------------------------------------------------------*/ 
typedef enum
{
	DATA_5_BITS = 5,
	DATA_6_BITS = 6,
	DATA_7_BITS = 7,
	DATA_8_BITS = 8
}ModbusRTU_DatabitsTypedef;

typedef enum
{
	PARITY_NONE = 0,
	PARITY_ODD,
	PARITY_EVEN
}ModbusRTU_ParityTypedef;

typedef enum
{
	STOP_ONE = 0,
	STOP_ONE_HALF,
	STOP_TWO
}ModbusRTU_StopbitsTypedef;

typedef enum
{
	STATE_RX,
	STATE_TX,
	STATE_IDLE,
	STATE_INIT_FAIL
}ModbusRTU_State;

typedef enum
{
	EVENT_NO = 0,
	EVENT_RECEIVED
}ModbusRTU_Event;

typedef enum
{
	ILLEGAL_FUNCTION         = 0x01,              /*<! Not support function code. */
	ILLEGAL_DATA_ADDR        = 0x02,                  
	ILLEGAL_DATA_VAL         = 0x03,                 
  SLAVE_DEVICE_FAILURE     = 0x04,
	ACKNOWLEDGE              = 0x05,              /*<! The slave device has received the request 
	                                                   and has started processing, , but it requires 
	                                                   more time to complete the operation. */
	SLAVE_DEVICE_BUSY        = 0x06,              /*<! The slave device is currently executing another 
	                                                   task and cannot immediately respond to the request.*/
	MEMORY_PARITY_ERROR      = 0x08,              /*<! The device encountered a memory parity error while 
	                                                   processing the request. */
	GATEWAY_PATH_UNAVALIABLE = 0x0A, 
	GATEWAY_TARGET_DEVICE_FAILED_TO_RESPONSE = 0x0B 
}ModbusRTU_ErrorCode;

typedef struct
{
	uint32_t                     Speed;            
	ModbusRTU_DatabitsTypedef    DataBits;
	ModbusRTU_ParityTypedef      Parity;
	ModbusRTU_StopbitsTypedef    StopBits;
	uint32_t                     idleUs;      /*<! When no data is received for more than idleUs microseconds, 
	                                            it is considered that data reception is complete. */
}ModbusRTU_InitTypedef;

/* Exported functions --------------------------------------------------------*/
extern bool ModbusRTU_Init(const ModbusRTU_InitTypedef * initTypedef);
extern uint16_t ModbusRTU_CRC16( uint8_t * pucFrame, uint16_t usLen );
extern void ModbusRU_SetSlaveAddress( uint8_t slaveAddress );
extern void ModbusRTU_EventLoopStart( void );

extern void ModbusRTU_UsartReceiveHandler( void );
extern void ModbusRTU_TimerExpiredHandler( void );

#endif /* __BMS_HAL_COMM_H */